use crate::service::user_service::UserService;
use actix_web::web::{Json};
use actix_web::{delete, get, post, put};
use actix_web::{HttpRequest, HttpResponse};

use fast_common::common::api_result::Api;
use fast_common::models::user::{SysUser, UserVo};
use fast_common::models::user_role::{DefaultRole, UserRolesReq};
use crate::service::role_service::RoleService;

///
/// ```添加用户```
///
#[post("new")]
pub async fn new_user(arg: Json<SysUser>, _request: HttpRequest) -> HttpResponse {
    let result = UserService::add(arg.into_inner()).await;
    return Api::from_any_result(result).await.to_response_of_json().await;
}

///
/// ```获取用户列表```
///
#[get("list")]
pub async fn list_user(arg: Json<UserVo>, _request: HttpRequest) -> HttpResponse {
    let list = UserService::list(arg.into_inner()).await;
    return Api::from_any_result(list).await.to_response_of_json().await;
}

///
/// ```修改用户信息```
///
#[put("update")]
pub async fn update_user(arg: Json<SysUser>, _request: HttpRequest) -> HttpResponse {
    let result = UserService::update(arg.into_inner()).await;
    return Api::from_any_result(result).await.to_response_of_json().await;
}

///
/// ```删除用户信息```
///
#[delete("delete")]
pub async fn delete_user(arg: Json<SysUser>) -> HttpResponse {
    let result = UserService::delete(arg.into_inner()).await;
    return Api::from_any_result(result).await.to_response_of_json().await;
}

///
///```用户添加角色```
///
#[post("auth_role")]
pub async fn auth_role(arg: Json<UserRolesReq>) -> HttpResponse {
    if arg.roles_id.is_empty() {
        return Api::<()>::error(String::from("未选择角色")).await.to_response_of_json().await;
    }
    let result = UserService::auth_role(arg.into_inner()).await;
    return Api::from_any_result(result).await.to_response_of_json().await;
}


///
/// ```用户选择默认角色```
///
#[post("choose_default_role")]
pub async fn choose_default_role(arg: Json<DefaultRole>) -> HttpResponse {
    if arg.default_role_id == 0 {
        return Api::<()>::error(String::from("请选择身份登录")).await.to_response_of_json().await;
    }
    let result = RoleService::choose_default_role(arg.into_inner()).await;
    return Api::from_any_result(result).await.to_response_of_json().await;
}